/*
 * @FilePath: \undefinedc:\Users\sxjct\.leetcode\39.组合总和.cpp
 * @Brief: 
 * @Version: 1.0
 * @Date: 2021-03-22 14:20:26
 * @Author: tianyiyi
 * @Copyright: Copyright@tianyiyi
 * @LastEditors: Mr.Tian
 * @LastEditTime: 2021-03-22 14:35:49
 */
/*
 * @lc app=leetcode.cn id=39 lang=cpp
 *
 * [39] 组合总和
 */

// @lc code=start
class Solution {
private:
    vector<vector<int>> res;
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<int> path;
        backtrack(candidates , path,target,0);
        return res;
    }
    
    void backtrack(vector<int>& candidates, vector<int>& path,int target,int start)
    {
        int sum = accumulate(path.begin(),path.end(),0);
        if(sum == target)
            res.push_back(path);
        else if(sum > target)
        {
            return;
        }
        for(int i = start ; i < candidates.size() ; i++)
        {
            path.push_back(candidates[i]);
            backtrack(candidates,path,target,i);
            path.pop_back();
        }
    }
};
// @lc code=end

